home *** CD-ROM | disk | FTP | other *** search
- /* Screen editor: main program module
- *
- * Module: ed2/ccc
- * Date: November 14, 1983
- * Changed: February 29, 1984
- * Changed: March 8, 1984
- * Changed: March 10, 1984
- */
-
- #include ed0
- #include ed1
-
- /* the main program dispatches the routines
- * that handle the various modes. */
-
- #define CMNDMODE 1 /* enter command mode flag */
- #define INSMODE 2 /* enter insert mode flag */
- #define EDITMODE 3 /* enter edit mode flag */
- #define EXITMODE 4 /* exit editor flag */
-
- main()
- { char mode;
- /* fmt output by default goes to screen */
- fmtassn(NO);
- /* set tabs, clear the screen and sign on */
- fmtset(4);
- outclr();
- outxy(14,10);
- fmtsout("VMED 2.2c -- Ed Ream's Editor, PLUS",0);
- outxy(15,12);
- fmtsout("Virtual Memory by Jim Kyle, et al",0);
- outxy(0,1);
- pmtmode("");
- /* clear the main buffer */
- name(""); /* clear filename */
- bufnew();
- bufgo(1);
- /* start off in command mode */
- mode = CMNDMODE;
- /* get null line 1 for edit() */
- edgetln();
- FOREVER {
- if (mode==EXITMODE)
- break;
- switch(mode) {
- case CMNDMODE:
- mode = command();
- break;
- case EDITMODE:
- mode = edit();
- break;
- case INSMODE:
- mode = insert();
- break;
- }
- }
- }
-
- /*
- * handle edit mode
- * dispatch the proper routine based on
- * one-character commands
- */
- edit()
- { char c;
- int v;
- /* we can't do edgetln() or edgo() here because those
- * calls reset the cursor.
- */
- if (!in_txt()) return(INSMODE);
- pmtedit();
- FOREVER {
- /* get command */
- if ((c=tolower(syscin())) == deletekey)
- c = ZAP1;
- if (c==ESC1)
- return(CMNDMODE); /* enter command mode */
- else if ((c==INS1)|(c==insertkey))
- return(INSMODE); /* enter insert mode */
- else if (special(c)==YES) {
- if (c==UP1 || c==DOWN1)
- return(INSMODE);
- else
- continue;
- }
- else if (control(c)==YES)
- continue;
- else switch(c) {
- /* remember line number -- added 2/29/84 */
- case recallkey:
- rememln(bufln());
- pmtlnx();
- break;
- /* cursor right */
- case crightkey:
- edright();
- pmtcol();
- break;
- /* move to bottom of file */
- case bottomkey:
- edgo(buffree(),0);
- pmtlnx();
- break;
- /* change one character */
- case changekey:
- c = syscin();
- edchng(c);
- break;
- /* move to end of line */
- case endkey:
- edend();
- pmtcol();
- break;
- /* find */
- case findkey:
- edrepl();
- srch1();
- pmtmode("edit:");
- break;
- /* goto line number -- changed 2/16/84 */
- case gotokey:
- v=0;
- while (isdigit(c=syscin())&&(v<buffree()))
- v=10*v + (c-'0');
- if (v)
- edgo(v,0);
- else if (c=='r')
- edgo(rememln(0),0);
- pmtlnx();
- break;
- /* character change */
- case exchagkey:
- FOREVER {
- c=syscin();
- if (special(c)==YES || control(c)==YES)
- break;
- edchng(c);
- }
- break;
- /* kill characters */
- case killkey:
- c=syscin();
- if ((special(c)==NO)&(control(c)==NO))
- edkill(c);
- pmtcol();
- break;
- /* beginning of line */
- case beginkey:
- edbegin();
- pmtcol();
- break;
- /* move down one page */
- case pagedown:
- edgo(bufln()+(SCRNL-2),0);
- pmtlnx();
- break;
- /* replace a line */
- case replackey:
- edkill(0xff);
- return(INSMODE);
- /* search for char on line */
- case searchkey:
- c=syscin();
- if ((special(c)==NO)&(control(c)==NO))
- edsrch(c);
- pmtcol();
- break;
- /* top of file */
- case topkey:
- edgo(1,0);
- pmtlnx();
- break;
- /* move up one page */
- case pageup:
- if(bufln() >= SCRNL)
- edgo(bufln()-(SCRNL-2),0);
- else edgo(1,0);
- pmtlnx();
- break;
- /* go to end of line and insert */
- case extendkey:
- edend();
- return(INSMODE);
- }
- /* do nothing if command not found */
- }
- }
-
- /* insert mode */
- insert()
- { char c;
- pmtmode("insert:");
- FOREVER {
- /* get command */
- c=syscin();
- if (c==ESC1)
- return(CMNDMODE); /* enter command mode */
- else if (c==EDIT1)
- return(EDITMODE); /* enter edit mode */
- else if (c==INS1); /* do nothing */
- else if (special(c)==YES) {
- if (c==UP2 || c==DOWN2)
- return(EDITMODE);
- else
- continue;
- }
- else if (control(c)==YES)
- /* ignore non-special control characters */
- continue;
- else {
- /* insert one char in line */
- edins(c);
- pmtcol();
- }
- }
- }
-
- /* return YES if c is a control char */
- control(c) char c;
- { if (c==TAB) return(NO); /* tab is regular */
- else if (c>=127) return(YES); /* del or high bit on */
- else if (c<32) return(YES); /* control character */
- else return(NO);
- }
-
- /*
- * handle the default actions of all special keys
- * return YES if c is one of the keys
- */
- special(c) char c;
- { switch(c) {
- case JOIN1:
- edjoin();
- pmtline();
- return(YES);
- case SPLT1:
- edsplit();
- pmtline();
- return(YES);
- case ABT1:
- edabt();
- pmtcol();
- return(YES);
- case DEL1:
- eddel();
- pmtcol();
- return(YES);
- case ZAP1:
- edzap();
- pmtline();
- return(YES);
- case UP2:
- /* move up */
- edup();
- pmtlnx();
- return(YES);
- case UP1:
- /* insert up */
- ednewup();
- pmtline();
- return(YES);
- case DOWN2:
- /* move down */
- eddn();
- pmtlnx();
- return(YES);
- case DOWN1:
- /* insert down */
- ednewdn();
- pmtline();
- return(YES);
- case LEFT1:
- edleft();
- pmtcol();
- return(YES);
- case RIGHT1:
- edright();
- pmtcol();
- return(YES);
- default:
- return(NO);
- }
- }
-
- /*
- * command() dispatches command routines while in command mode
- */
- command()
- { int v,topline,ypos,oldline,k;
- char c,args[SCRNW1],*argp,flag;
- /* command mode commands may move the current line.
- * command mode must save the current line on entry
- * and restore it on exit.
- */
- edrepl();
- /* remember how the screen was drawn on entry */
- oldline=bufln();
- ypos=outgety();
- topline=oldline-ypos+1;
- flag = 0;
- FOREVER {
- outxy(0,SCRNL1);
- if(flag == 0)
- fmtcrlf();
- flag = 1;
- pmtmode("command:");
- fmtsout(">",0);
- getcmnd(args,1);
- fmtcrlf();
- c=args[0];
- /* parse the command given */
- if(c == EOS)
- continue;
- else if ((c==EDIT1)|(c==INS1)) {
- if (oldline==bufln()) {
- edgetln();
- bufout(topline,1,SCRNL1);
- outxy(0,ypos);
- }
- else edgo(bufln(),0);
- if (c==EDIT1) return(EDITMODE);
- else return(INSMODE);
- }
- else if (lookup(args,"append")) { /* 2-29-84 */
- append(args);
- bufgo(oldline);
- }
- else if (lookup(args,"paste")) {
- paste();
- bufgo(oldline);
- }
- else if (lookup(args,"keep") || lookup(args,"hold"))
- keep(args); /* 2-29-84 */
- else if (tolower(args[0])=='g') {
- argp=skipbl(args+1);
- if (argp[0]==EOS) {
- edgo(oldline,0);
- return(EDITMODE);
- }
- else if (number(argp,&v)==YES) {
- edgo(v,0);
- return(EDITMODE);
- }
- else outm(badnum);
- }
- else if (lookup(args,"line")) {
- argp=skiparg(args);
- argp=skipbl(argp);
- if (argp[0]==EOS)
- edgo(oldline,0);
- else if (number(argp,&v)==YES) {
- oldline = v;
- edgo(v,0);
- }
- else outm(badnum);
- }
- else if (lookup(args,"put"))
- putfile(args);
- else if (lookup(args,"change"))
- change(args);
- else if (lookup(args,"new"))
- clear();
- else if (lookup(args,"delete")||lookup(args,"del"))
- delete(args);
- else if (lookup(args,"dos")||lookup(args,"quit")) {
- if(chkbuf()==YES)
- return(EXITMODE);
- }
- else if (lookup(args,"find")) {
- if ((k=find()) >= 0) {
- edgo(bufln(),k);
- return(EDITMODE);
- }
- else {
- /* get current line */
- bufgo(oldline);
- edgetln();
- /* stay in command mode */
- outm(patnotfnd);
- }
- }
- else if (lookup(args,"list"))
- list(args);
- else if (lookup(args,"print")) {
- fmtassn(YES);
- list(args);
- fmtassn(NO);
- }
- else if (lookup(args,"load")) {
- if(load(args) == YES) {
- edgo(0,0);
- return(EDITMODE);
- }
- }
- else if (lookup(args,"name"))
- name(args);
- else if (lookup(args,"save"))
- save(args);
- else if (lookup(args,"search"))
- search(args);
- else if (lookup(args,"tab"))
- tabs(args);
- else if (lookup(args,"copy"))
- copy(args,NO);
- else if (lookup(args,"move"))
- copy(args,YES);
- else if (lookup(args,"top")) {
- edgo(1,0);
- return(EDITMODE);
- }
- else if (lookup(args,"bottom")) {
- edgo(HUGE,0);
- return(EDITMODE);
- }
- else
- outm(cmdfnd);
- }
- }
-
- /* return YES if line starts with command */
- lookup(line,command) char *line,*command;
- { skipbl(line);
- while(*command)
- if (tolower(*line++)!=*command++)
- return(NO);
- if ((*line==EOS)||(*line==' ')||(*line==TAB))
- return(YES);
- else return(NO);
- }
-
- /* get next command into argument buffer */
- getcmnd(args,offset) char *args,offset;
- { char c,j,k;
- outxy(offset,outgety());
- outdeol();
- k=0;
- while ((c=syscin())!=CR) {
- if ((c==EDIT1)|(c==INS1)) {
- *args=c;
- return;
- }
- if ((c==DEL1)|(c==LEFT1)) {
- if (k>0) {
- outxy(offset,outgety());
- outdeol();
- --k;
- j=0;
- while (j<k)
- outchar(args[j++]);
- }
- }
- else if (c==ABT1) {
- outxy(offset,outgety());
- outdeol();
- k=0;
- }
- else if ((c != TAB) & (c < ' '))
- continue; /* do nothing */
- else if ((k + offset) < SCRNW1) {
- args[k++]=c;
- outchar(c);
- }
- }
- args[k]=EOS;
- }
-
- /* end module ed2/ccc */
-
-